import { useReducer } from "react";
import LoaderContext from "./LoaderContext";

export const reducer = (state, { type, payload }) => {
  switch (type) {
    case "status":
    default:
      return {
        visible: payload.visible,
      };
  }
};

export  const LoaderState = (props) => {
  const [loader, dispatch] = useReducer(reducer, {
    visible: true,
  });
 const updateLoaderStatus = (status) => {
    const fn = () => {
      dispatch({
        type: "status",
        payload: { visible: status },
      });
    };
    if (status === false) {
      setTimeout(() => {
       fn();
      }, 1000);
    } else {
      fn();
    }
  };

  return (
    <LoaderContext.Provider value={{ loader, updateLoaderStatus }}>
      {props.children}
    </LoaderContext.Provider>
  );
};

export default LoaderState;



